//
//  YZMBankCardListViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/7/12.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMBankCardListViewController.h"
#import "YZMBankCardListCell.h"
#import "YZMAlertView.h"
#import <ColorUtils/ColorUtils.h>

static NSString *const sYZMBankCardListCellID = @"YZMBankCardListCellID";

@interface YZMBankCardListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *bankCardList;
@property (nonatomic, strong) UIBarButtonItem *oldBarButton;

@end

@implementation YZMBankCardListViewController

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的银行卡";
    self.navigationItem.rightBarButtonItem = [YZMAppStyle rightBarItemWithImage:@"icon_add_card" target:self action:@selector(rightBarItemMethod)];
    self.oldBarButton = self.navigationItem.rightBarButtonItem;
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self createObjects];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bankCardUpdateNotification) name:YZMUserBankCardUpdateNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear: animated];
    
    if (!YZMUserServiceInstance.user.detail.isNamed) {
        
        YZMAlertView *alertView = [YZMAlertView alertViewForRecommandWithContent:@"您还未实名认证，请先认证"];
        alertView.cancelButton.hidden = NO;
        [alertView.sureButton setTitle:@"立即验证" forState:UIControlStateNormal];
        alertView.cancleHandle = ^{
            
            [YZMHCMInstance popViewController:nil animated:YES];
        };
        alertView.confirmHandle  = ^{
            
            [YZMHCMInstance pushViewControllerWithName:@"identifyVerify" andParams:nil];
            return YES;
        };
        [alertView showInView:self.navigationController.view];
        
        return;
    }
    
    [self bankCardUpdateNotification];
}


- (void)bankCardUpdateNotification {
    
    self.bankCardList = YZMUserServiceInstance.user.bankCardList;
    [self.tableView reloadData];
    
    if (self.bankCardList.count == 3) {
        
        self.navigationItem.rightBarButtonItem = nil;
    } else {
        
        self.navigationItem.rightBarButtonItem = self.oldBarButton;
    }
}

- (void)rightBarItemMethod {
    
    [YZMHCMInstance pushViewControllerWithName:@"addBankCard" andParams:@{@"pageName":@"addBankCard",@"vcName":self}];
}

- (void)createObjects {
    
    WS(ws)
    self.tableView = [[UITableView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self.tableView registerClass:[YZMBankCardListCell class] forCellReuseIdentifier:sYZMBankCardListCellID];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    UILabel *footerLB = [YZMAppStyle labelWithSmallLightGray];
    footerLB.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 24);
    footerLB.text = @"注：最多可以添加3张银行卡";
    footerLB.textAlignment = NSTextAlignmentCenter;
    self.tableView.tableFooterView = footerLB;
}

#pragma mark -- TableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.bankCardList.count == 3?3:self.bankCardList.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YZMBankCardListCell *cell = [tableView dequeueReusableCellWithIdentifier:sYZMBankCardListCellID forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.bankCardList.count == 0 || indexPath.section == self.bankCardList.count) {
        
        cell.item = nil;
    }else{
        
        cell.indexPath = indexPath;
        cell.item = self.bankCardList[indexPath.section];
    }

    return cell;
}

#pragma mark -- UItableViewDelete

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.bankCardList.count == 0 || indexPath.section == self.bankCardList.count) {
        
        [YZMHCMInstance pushViewControllerWithName:@"addBankCard" andParams:@{@"pageName":@"addBankCard",@"vcName":self}];
    } else {
        
        [YZMHCMInstance pushViewControllerWithName:@"modBankCard" andParams:@{@"bankCard":self.bankCardList[indexPath.section], @"indexPath":indexPath}];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 110;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 14;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 2 || section == self.bankCardList.count) {
        
        return 14;
    }
    
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [[UIView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
